package com.hyd.tweet.twitter.command;

import com.hyd.tweet.twitter.StatusFormatter;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.TwitterException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 根据关键字搜索推
 *
 * @author yiding.he
 */
public class SearchCommand extends Command {

    public static final int DEFAULT_SEARCH_COUNT = 20;

    @Override
    public List<String> processCommand() throws TwitterException {
        if (arguments.isEmpty()) {
            return Arrays.asList("——没有指定关键字。");
        }

        String keyword = arguments.get(0);

        Query query = new Query(keyword);
        query.setResultType(Query.RECENT);
        query.setCount(DEFAULT_SEARCH_COUNT);
        QueryResult result = twitter.search(query);

        List<String> list = new ArrayList<String>();

        for (Status status : result.getTweets()) {
            int id = user.pushToRoller(status);
            list.add(0, StatusFormatter.formatStatus(twitterUser, status, id));
        }

        list.add("——搜索\"" + keyword + "\"找到" + result.getTweets().size() + "条最近的查询结果。");
        return list;
    }
}
